package com.dragon.mmochat.service;

import java.awt.Color;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dragon.mmochat.MmochatConstant;
import com.dragon.mmochat.MmochatMain;
import com.dragon.mmochat.message.Mmochat_1_1S;
import com.dragon.mmochat.message.Mmochat_3_1S;
import com.dragon.mmochat.message.Mmochat_5_10S;
import com.dragon.mmochat.message.Mmochat_5_31S;
import com.dragon.mmochat.message.Mmochat_6_2S;
import com.dragon.mmochat.message.Mmochat_EnterMultiServerLeiTai;
import com.dragon.mmochat.message.Mmochat_EnterOtherServerMap;
import com.dragon.mmochat.message.Mmochat_SendHonorAwardToOldServer;
import com.dragon.mmochat.message.Mmochat_SendHonorChenHaoToOldServer;
import com.dragon.mmochat.message.Mmochat_SendMixWuDaoWordsToAllServer;
import com.dragon.mmochat.message.Mmochat_SendMultiServerWuDaoResult;
import com.dragon.mmochat.message.Mmochat_SetMixWuDaoWordsToServerList;
import com.dragon.mmochat.model.MmochatChenHao;
import com.dragon.mmochat.model.MmochatServerInfo;
import com.dragon.mmochat.model.MmochatTempUserInfo;
import com.dragon.mmochat.model.MmochatWuDaoWinWords;
import com.dragon.mmochat.model.battle.MmochatBattle;
import com.dragon.mmochat.model.enumType.MmochatChenHaoType;
import com.dragon.mmochat.model.kefu.MmochatKefuQuestion;
import com.dragon.mmochat.model.map.MmochatMap;
import com.dragon.mmochat.model.object.MmochatCommonObject;
import com.dragon.mmochat.model.object.MmochatPackageGrid;
import com.dragon.mmochat.model.role.MmochatFriend;
import com.dragon.mmochat.model.role.MmochatPet;
import com.dragon.mmochat.model.role.MmochatPlayer;
import com.dragon.mmochat.util.MmochatUtil;
import com.dragon.mmochat.util.SendBigPackUtil;
import com.skymobi.handler.SkymobiHandler;
import com.skymobi.message.widget.CtrlTLVStructureRequest;
import com.skymobi.service.SkymobiService;

//跨服战
public class MmochatMultiServerPkService {
	private static Logger log = LoggerFactory
			.getLogger(MmochatMainService.class);

	// skyId-->MmochatTempUserInfo:申请进入跨服专区的用户信息，只保留10分钟，供用户登陆专区时按skyId提取
	public static Map<Long, MmochatTempUserInfo> tmpUsers = new ConcurrentHashMap<Long, MmochatTempUserInfo>();

	public static ScheduledExecutorService checkTmpUsersThread = Executors
			.newScheduledThreadPool(1);

	public void init() {
		checkTmpUsersThread.scheduleWithFixedDelay(new CheckTmpUsers(), 10, 10,
				TimeUnit.MINUTES);
	}

	public class CheckTmpUsers implements Runnable {
		@Override
		public void run() {
			try {
				for (MmochatTempUserInfo userInfo : tmpUsers.values()) {
					if (userInfo.isTimeOut()) {
						tmpUsers.remove(userInfo.getSkyId());
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	// 登记参加跨服武道
	@SuppressWarnings("deprecation")
	@SkymobiService
	public void handleEnterOtherServerMap(SkymobiHandler handler,
			Mmochat_EnterOtherServerMap req) {

		// 判断是否为跨服武道时间
		Date now = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
		String curTimeString = sdf.format(now);

		if ((now.getDay() != 4) || now.getHours() != 19
				|| now.getMinutes() < 30) {
			MmochatUtil.sendCommonMsgbox(req.getConn(),
					"跨服武道入场时间:每周四的19:30-20:00,现在不是入场时间!当前跨服服务器时间为"
							+ curTimeString + "!如果您是从跨服专区掉线，10分钟内仍可进入跨服专区!");
			return;
		}

		// 离20:00还有多少时间
		now.setHours(20);
		now.setMinutes(0);
		long restTime = now.getTime() - System.currentTimeMillis();

		MmochatTempUserInfo userInfo = tmpUsers.get(req.getSkyId());
		if (userInfo == null) {
			userInfo = new MmochatTempUserInfo();
		}
		userInfo.setSkyId(req.getSkyId());
		userInfo.setRoleId(req.getRoleId());
		userInfo.setSrcInstId(req.getSrcInstId());
		userInfo.setSrcServerId(req.getSrcServerId());
		userInfo.setName(req.getName());
		userInfo.setLevel(req.getLevel());
		userInfo.setFamily(req.getFamily());
		userInfo.setStartTick(System.currentTimeMillis());
		userInfo.setLife(Math.min(restTime, MmochatConstant.roleLeaveGameTime));
		tmpUsers.put(userInfo.getSkyId(), userInfo);

		MmochatUtil.sendCommonMsgbox(req.getConn(), "申请完成!请在10分钟内，"
				+ MmochatUtil.wrapColor("退出本区，登陆<"
						+ MmochatUtil.wrapColor("跨服专区", Color.red) + ">进入战场。",
						Color.yellow) + "20:00后将无法进入!");
	}

	// 登陆跨服专区
	public static void enterMultiServer(MmochatPlayer role, boolean isInBuffer,
			int hallState, int mapId, int line, int posX, int posY, String hint) {
		if (isInBuffer) {
			// 如果此角色已经在游戏中了,允许再次进场
			role.setDeadConnection(false);
			MmochatMap curMap = MmochatMapService
					.getMapByMapId(role.getMapId());
			if (curMap == null) {
				// 地图不存在,可能之前所在临时地图已经关闭
				MmochatUtil.sendCommonMsgbox(role, "目标地图已关闭，可能是活动已经结束!");
				return;
			}

			// 更新状态
			role.setLatestActiveTime(System.currentTimeMillis());

			// 停止交易
			role.stopDeal();

			// 清除周围玩家
			role.getSceenRoles().clear();

			// 清除坐标缓存器
			role.getPositionBuffer().clear();

			// 清除NPC列表
			role.getTmpNpcs().clear();

			// 初始化一些状态
			role.initNormalEnterGame();
			role.setHasCheckStorePwd(false);

			// 重新设置包裹中物品ID
			for (MmochatPackageGrid grid : role.getMyPackage().values()) {
				if (grid != null) {
					MmochatCommonObject obj = grid.getObject();
					if (obj != null) {
						obj.setNewId();
						if (obj.getEquipUserRoleId() != null) {
							obj.setEquipUserRoleId(role.getRoleId());
						}
					}
				}
			}
			// 重新设置宠物ID
			List<MmochatPet> pets = new ArrayList<MmochatPet>();
			for (MmochatPet pet : role.getPets().values()) {
				if (pet != null) {
					pet.setNewId();
					pets.add(pet);
				}
			}
			role.getPets().clear();
			for (MmochatPet pet : pets) {
				role.addPet(pet);
			}

			// 重新设置好友roleId
			int regServerId = MmochatMain.getServerIdByInstanceId(role
					.getRegServerInstId());
			Map<Integer, MmochatFriend> myDearFriends = new ConcurrentHashMap<Integer, MmochatFriend>();
			for (MmochatFriend frd : role.getMyDearFriends().values()) {
				frd.setRoleId(frd.getRoleId() + 10000000 * regServerId);
				myDearFriends.put(frd.getRoleId(), frd);
			}
			role.setMyDearFriends(myDearFriends);

			Map<Integer, MmochatFriend> myEnermys = new ConcurrentHashMap<Integer, MmochatFriend>();
			for (MmochatFriend frd : role.getMyEnermys().values()) {
				frd.setRoleId(frd.getRoleId() + 10000000 * regServerId);
				myEnermys.put(frd.getRoleId(), frd);
			}
			role.setMyEnermys(myEnermys);

			// 设置区服称号
			MmochatServerInfo server = MmochatServerListService.serverInstanceMap
					.get(role.getRegServerInstId());
			if (server != null) {
				role.getChenHaoMap().clear();
				MmochatChenHao chenhao = new MmochatChenHao();
				chenhao.setType(MmochatChenHaoType.区服称号);
				chenhao.setName(server.getName());
				chenhao.setInUse(true);
				role.getChenHaoMap().put(chenhao.getType(), chenhao);
			}

			// 设置myFans的roleId
			Map<Integer, Integer> myFans = new ConcurrentHashMap<Integer, Integer>();
			for (Integer myFanRoleId : role.getMyFans().values()) {
				myFans.put(myFanRoleId + 10000000 * regServerId, myFanRoleId
						+ 10000000 * regServerId);
			}
			role.setMyFans(myFans);

			// 通知好友我上线了
			for (Integer roleId1 : role.getMyFans().values()) {
				MmochatPlayer friend = MmochatMainService.players.get(roleId1);
				if (friend == null) {
					continue;
				}
				MmochatFriend friendShip = friend.getMyDearFriends().get(
						role.getRoleId());
				if (friendShip != null) {
					// 上线消息
					friendShip.setBmpId(role.getBmpId());
					friendShip.setName(role.getName());
					friendShip.setMoodInfo(role.getMoodInfo());
					friend.addOnlineFriend(role.getRoleId());
					if (friend.getMyIdols().containsKey(role.getRoleId())) {
						MmochatChatService.sendSystemMsgToPerson(friend, "您的好友"
								+ MmochatUtil.wrapColor(role.getName(),
										Color.green) + "上线了!");
					}
				}
			}

			// 下发快捷操作
			CtrlTLVStructureRequest quickStartPack = MmochatMainService
					.getQuickStart(hallState, role.getRoleId());
			MmochatUtil.sendCommonPack(role, quickStartPack);

			// 下发好友列表
			MmochatFriendService.sendFrindListToPerson(role);

			// 下发包裹
			SendBigPackUtil
					.sendBigPack_5_1S(role, role.getMyPackage().values());

			// 下发我的游戏币数量
			Mmochat_5_31S moneyPack = new Mmochat_5_31S();
			moneyPack.setSmallMoney((int) role.getSmall_money());
			MmochatUtil.sendCommonPack(role, moneyPack);

			// 下发宠物列表
			Mmochat_5_10S petPack = new Mmochat_5_10S();
			petPack.setReset(true);
			petPack.setPets(new ArrayList<MmochatPet>(role.getPets().values()));
			MmochatUtil.sendCommonPack(role, petPack);

			// 下发我的角色信息
			Mmochat_6_2S roleInfoPack = new Mmochat_6_2S();
			roleInfoPack.setPlayer(role);
			MmochatUtil.sendCommonPack(role, roleInfoPack);

			// 进入游戏
			Mmochat_1_1S pack = new Mmochat_1_1S();
			pack.setMap(curMap);
			pack.setLine(role.getLine());
			pack.setMe(role);
			MmochatMainService.enterGame(role, role.getUserConnInfo(), pack);
			MmochatUtil.sendCommonPack(role, pack);

			// 如果是在战斗状态,则下发战场
			if (role.isInBattle()) {
				MmochatBattle battle = MmochatBattleService.battles.get(role
						.getBattleId());
				if (battle != null && !battle.isOver()) {
					Mmochat_3_1S pack11 = new Mmochat_3_1S();
					pack11.setBattle(battle);
					pack11.setMapId(role.getMapId());
					pack11.setLine((byte) role.getLine());
					Byte myPos = battle.getPos(role);
					if (myPos != null) {
						pack11.setMyPos(myPos);
						MmochatPet pet = role.getPetInBattle();
						if (pet != null) {
							Byte petPos = battle.getPos(pet);
							if (petPos != null) {
								pack11.setPetPos(petPos);
							}
						}
						MmochatUtil.sendCommonPack(role, pack11);
						return;
					}
				} else {
					role.leaveBattle();
				}
			}

			// 如果是在观战状态,则下发观战战场
			if (role.isInWatchingBattle()) {
				MmochatBattle battle = MmochatBattleService.battles.get(role
						.getWatchBattleId());
				if (battle != null && !battle.isOver()) {
					Mmochat_3_1S pack11 = new Mmochat_3_1S();
					pack11.setBattle(battle);
					pack11.setMapId(role.getMapId());
					pack11.setLine((byte) role.getLine());
					pack11.setAudience(true);
					pack11.setMyPos((byte) -1);
					MmochatUtil.sendCommonPack(role, pack11);
					return;
				} else {
					role.leaveWatchingBattle();
				}
			}

			// 离线消息
			MmochatFriendService.sendOfflineMsgToPerson(role);
			return;
		} else {
			// 设置地图、线路、坐标
			role.setMapId(mapId);
			role.setLine(line);
			role.setX(posX);
			role.setY(posY);

			// 检查同skyId的其它角色是否已经登陆
			for (MmochatPlayer me1 : MmochatMainService.players.values()) {
				if (me1.getSkyId() == role.getSkyId()
						&& me1.getRoleId() != role.getRoleId()) {
					MmochatUtil.sendCommonMsgbox(role, "您的角色"
							+ MmochatUtil
									.wrapColor(me1.getName(), Color.yellow)
							+ "目前正在游戏内活动，请登陆此角色!");
					return;
				}
			}

			if (MmochatMainService.players.size() >= MmochatConstant.maxRolePerServer) {
				MmochatUtil.sendCommonMsgbox(role, "服务器总人数已满,请稍候再进!");
				return;
			}

			// 从内存中移除玩家角色
			MmochatMainService.offlinePlayers.remove(role.getRoleId());
			MmochatGmService.gmOptionPlayers.remove(role.getRoleId());
			MmochatMainService.offlinePlayers_skyIdMap.remove(role.getSkyId());

			if (role.getImei() != null
					&& MmochatConstant.forbiddinImeiList.containsKey(role
							.getImei())) {
				MmochatUtil.sendCommonMsgbox(role,
						"您由于在游戏中进行了破坏游戏环境的活动，已被禁止进入游戏!");
				return;
			}

			MmochatStatisticsService.todayLoginNum.put(role.getRoleId(),
					(byte) 0);

			// 初始化一些状态
			role.initNormalEnterGame();
			role.setHasCheckStorePwd(false);

			MmochatMap curMap = MmochatMapService
					.getMapByMapId(role.getMapId());
			if (curMap == null) {
				// 地图不存在
				MmochatUtil.sendCommonMsgbox(role, "战场地图稍未准备好，请稍候再进!");
				return;
			}

			role.setLastTimeOfWorldChat(System.currentTimeMillis()
					- MmochatConstant.minIntervalOfWorldChat);

			Mmochat_1_1S pack = new Mmochat_1_1S();
			pack.setMap(curMap);
			pack.setLine(line);
			pack.setMe(role);

			MmochatMainService.enterGame(role, role.getUserConnInfo(), pack);

			role.stopDeal();

			// 重新设置包裹中物品ID
			for (MmochatPackageGrid grid : role.getMyPackage().values()) {
				if (grid != null) {
					MmochatCommonObject obj = grid.getObject();
					if (obj != null) {
						obj.setNewId();
						if (obj.getEquipUserRoleId() != null) {
							obj.setEquipUserRoleId(role.getRoleId());
						}
					}
				}
			}
			// 重新设置宠物ID
			List<MmochatPet> pets = new ArrayList<MmochatPet>();
			for (MmochatPet pet : role.getPets().values()) {
				if (pet != null) {
					pet.setNewId();
					pets.add(pet);
				}
			}
			role.getPets().clear();
			for (MmochatPet pet : pets) {
				role.addPet(pet);
			}

			// 重新设置好友roleId
			int regServerId = MmochatMain.getServerIdByInstanceId(role
					.getRegServerInstId());
			Map<Integer, MmochatFriend> myDearFriends = new ConcurrentHashMap<Integer, MmochatFriend>();
			for (MmochatFriend frd : role.getMyDearFriends().values()) {
				frd.setRoleId(frd.getRoleId() + 10000000 * regServerId);
				myDearFriends.put(frd.getRoleId(), frd);
			}
			role.setMyDearFriends(myDearFriends);

			Map<Integer, MmochatFriend> myEnermys = new ConcurrentHashMap<Integer, MmochatFriend>();
			for (MmochatFriend frd : role.getMyEnermys().values()) {
				frd.setRoleId(frd.getRoleId() + 10000000 * regServerId);
				myEnermys.put(frd.getRoleId(), frd);
			}
			role.setMyEnermys(myEnermys);

			// 设置区服称号
			MmochatServerInfo server = MmochatServerListService.serverInstanceMap
					.get(role.getRegServerInstId());
			if (server != null) {
				role.getChenHaoMap().clear();
				MmochatChenHao chenhao = new MmochatChenHao();
				chenhao.setType(MmochatChenHaoType.区服称号);
				chenhao.setName(server.getName());
				chenhao.setInUse(true);
				role.getChenHaoMap().put(chenhao.getType(), chenhao);
			}

			// 设置myFans的roleId
			Map<Integer, Integer> myFans = new ConcurrentHashMap<Integer, Integer>();
			for (Integer myFanRoleId : role.getMyFans().values()) {
				myFans.put(myFanRoleId + 10000000 * regServerId, myFanRoleId
						+ 10000000 * regServerId);
			}
			role.setMyFans(myFans);

			// 通知好友我上线了
			for (Integer roleId1 : role.getMyFans().values()) {
				MmochatPlayer friend = MmochatMainService.players.get(roleId1);
				if (friend == null) {
					continue;
				}
				MmochatFriend friendShip = friend.getMyDearFriends().get(
						role.getRoleId());
				if (friendShip != null) {
					// 上线消息
					friendShip.setBmpId(role.getBmpId());
					friendShip.setName(role.getName());
					friendShip.setMoodInfo(role.getMoodInfo());
					friend.addOnlineFriend(role.getRoleId());
					if (friend.getMyIdols().containsKey(role.getRoleId())) {
						MmochatChatService.sendSystemMsgToPerson(friend, "您的好友"
								+ MmochatUtil.wrapColor(role.getName(),
										Color.green) + "上线了!");
					}
				}
			}

			// 下发快捷操作
			CtrlTLVStructureRequest quickStartPack = MmochatMainService
					.getQuickStart(hallState, role.getRoleId());
			MmochatUtil.sendCommonPack(role, quickStartPack);

			// 下发好友列表
			MmochatFriendService.sendFrindListToPerson(role);

			// 下发包裹
			SendBigPackUtil
					.sendBigPack_5_1S(role, role.getMyPackage().values());

			// 下发我的游戏币数量
			Mmochat_5_31S moneyPack = new Mmochat_5_31S();
			moneyPack.setSmallMoney((int) role.getSmall_money());
			MmochatUtil.sendCommonPack(role, moneyPack);

			// 下发宠物列表
			Mmochat_5_10S petPack = new Mmochat_5_10S();
			petPack.setReset(true);
			petPack.setPets(new ArrayList<MmochatPet>(role.getPets().values()));
			MmochatUtil.sendCommonPack(role, petPack);

			// 下发我的角色信息
			Mmochat_6_2S roleInfoPack = new Mmochat_6_2S();
			roleInfoPack.setPlayer(role);
			MmochatUtil.sendCommonPack(role, roleInfoPack);

			MmochatUtil.sendCommonPack(role, pack);

			// 下发欢迎提示语
			if (hint != null) {
				MmochatChatService.sendSystemMsgToPerson(role, hint);
			}

			// 如果有客服消息，则下发客服消息
			for (MmochatKefuQuestion question : MmochatGmService.kefuMsg) {
				if (question.isReplied()
						&& question.getRoleId() == role.getRoleId()) {
					// 问题已答复，下发给玩家
					String content = question.getLastContent();
					if (content == null) {
						continue;
					}
					MmochatGmService.sendKefuMesboxToPerson(role, question
							.getId(), content, question.isNeedReply());
				}
			}

			// 检查道具是否过期
			MmochatPropService.checkRoleProp(role);

			// 离线消息
			MmochatFriendService.sendOfflineMsgToPerson(role);

			return;
		}
	}

	// 登记参加跨服擂台
	@SuppressWarnings("deprecation")
	@SkymobiService
	public void handleEnterMultiServerLeiTai(SkymobiHandler handler,
			Mmochat_EnterMultiServerLeiTai req) {

		if (!MmochatConstant.isMultiLeiTaiOpen) {
			MmochatUtil.sendCommonMsgbox(req.getConn(),
					"跨服擂台暂时关闭，开放时间请关注游戏内公告!");
			return;
		}

		// 判断是否为跨服擂台时间
		Date now = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
		String curTimeString = sdf.format(now);

		if (now.getHours() > 18 && now.getHours() < 23) {
			// 19点、20点、21点、22点不允许进入
			MmochatUtil.sendCommonMsgbox(req.getConn(),
					"跨服擂台入场时间:每天的0:00-19:00和23:00-24:00,现在不是入场时间!当前跨服服务器时间为"
							+ curTimeString + "!如果您是从跨服专区掉线，10分钟内仍可进入跨服专区!");
			return;
		}

		// 离19:00还有多少时间
		now.setHours(19);
		now.setMinutes(0);
		long restTime = now.getTime() - System.currentTimeMillis();
		if (restTime < 0) {
			restTime = MmochatConstant.roleLeaveGameTime;
		}

		MmochatTempUserInfo userInfo = tmpUsers.get(req.getSkyId());
		if (userInfo == null) {
			userInfo = new MmochatTempUserInfo();
		}
		userInfo.setSkyId(req.getSkyId());
		userInfo.setRoleId(req.getRoleId());
		userInfo.setSrcInstId(req.getSrcInstId());
		userInfo.setSrcServerId(req.getSrcServerId());
		userInfo.setName(req.getName());
		userInfo.setLevel(req.getLevel());
		userInfo.setFamily(req.getFamily());
		userInfo.setStartTick(System.currentTimeMillis());
		userInfo.setLife(Math.min(restTime, MmochatConstant.roleLeaveGameTime));
		tmpUsers.put(userInfo.getSkyId(), userInfo);

		MmochatUtil.sendCommonMsgbox(req.getConn(), "申请完成!请在10分钟内，"
				+ MmochatUtil.wrapColor(
						"退出本区，登陆<" + MmochatUtil.wrapColor("跨服专区", Color.red)
								+ ">进入跨服擂台。", Color.yellow));
	}

	// 增加声望
	@SkymobiService
	public void handleC_SendHonorAwardToOldServer(SkymobiHandler handler,
			Mmochat_SendHonorAwardToOldServer req) {
		MmochatPlayer me = MmochatGmService.getRoleInfoById(req.getRoleId());
		if (me != null) {
			synchronized (me) {
				me.addHonor(req.getAddHonor());
				try {
					MmochatDao.setHonor(me.getHonor(), me.getRoleId());
				} catch (Exception e) {
					e.printStackTrace();
				}
				log.debug("增加" + me.getName() + "声望，当前声望：" + me.getHonor());
			}
		}
	}

	// 增加声望和称号
	@SkymobiService
	public void handleC_SendHonorChenHaoToOldServer(SkymobiHandler handler,
			Mmochat_SendHonorChenHaoToOldServer req) {
		MmochatPlayer me = MmochatGmService.getRoleInfoById(req.getRoleId());
		if (me != null) {
			synchronized (me) {
				me.addHonor(req.getAddHonor());
				try {
					MmochatDao.setHonor(me.getHonor(), me.getRoleId());
				} catch (Exception e) {
					e.printStackTrace();
				}
				log.debug("增加" + me.getName() + "声望，当前声望：" + me.getHonor());
			}
			MmochatChenHaoType type = null;
			String extraInfo = "";
			switch (req.getIndex()) {
			case 0:
				type = MmochatChenHaoType.跨服王者;
				extraInfo = "跨服武道第一名获得的称号!";
				break;
			case 1:
				type = MmochatChenHaoType.跨服高手;
				extraInfo = "跨服武道第二名获得的称号!";
				break;
			case 2:
				type = MmochatChenHaoType.跨服新贵;
				extraInfo = "跨服武道第三名获得的称号!";
				break;
			default:
				break;
			}
			if (type != null) {
				// 删除其它跨服称号
				MmochatChenHao.delAndUpdateChenHaoAndSaveSql(me.getRoleId(),
						MmochatChenHaoType.跨服王者);
				MmochatChenHao.delAndUpdateChenHaoAndSaveSql(me.getRoleId(),
						MmochatChenHaoType.跨服高手);
				MmochatChenHao.delAndUpdateChenHaoAndSaveSql(me.getRoleId(),
						MmochatChenHaoType.跨服新贵);

				MmochatChenHao chenHao = new MmochatChenHao();
				chenHao.setType(type);
				chenHao.setExtraEffectInfo(extraInfo);
				chenHao.setLifeTime(MmochatConstant.MS_PER_DAY * 6);
				MmochatChenHao.addAndUpdateChenHaoAndSaveSql(me.getRoleId(),
						chenHao);
			}
		}
	}

	// 武道比赛结果
	@SkymobiService
	public void handleC_SendMultiServerWuDaoResult(SkymobiHandler handler,
			Mmochat_SendMultiServerWuDaoResult req) {
		String title = req.getTitle();
		String content = req.getContent();
		try {
			MmochatDao.addEventLog(req.getEventType(), System
					.currentTimeMillis(), title, content);
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		// 重新加载
		MmochatNpcService.reloadEventLog();
		log.debug("收到跨服武道结果，" + title + "区获胜");
	}

	// 列表服务器收到跨服宣言，将此发送给各个区服
	@SkymobiService
	public void handleSetMixWuDaoWordsToServerList(SkymobiHandler handler,
			Mmochat_SetMixWuDaoWordsToServerList req) {
		// 获取区服名
		String serverName = "";
		int roleId = req.getRoleId();
		MmochatServerInfo server = MmochatServerListService.serverInstanceMap
				.get(req.getSrcInstId());
		if (server != null) {
			serverName = server.getName();
			roleId += server.getId() * 10000000;
		}
		String name = req.getName();
		String word = req.getWords();
		int levelRange = req.getLevelRange();

		Mmochat_SendMixWuDaoWordsToAllServer pack = new Mmochat_SendMixWuDaoWordsToAllServer();
		pack.setServerName(serverName);
		pack.setRoleId(roleId);
		pack.setName(name);
		pack.setWord(word);
		pack.setLevelRange(levelRange);
		MmochatUtil.sendCommonPackToAllServer(pack);
	}

	// 各区服收到列表服务器发送的跨服宣言
	@SuppressWarnings("deprecation")
	@SkymobiService
	public void handleSendMixWuDaoWordsToAllServer(SkymobiHandler handler,
			Mmochat_SendMixWuDaoWordsToAllServer req) {
		// 判定日期
		long deadTime = 0;
		Date now = new Date();
		if (now.getDay() == 4 || now.getDay() == 5) {
			// 周四周五可以设置
			now.setHours(23);
			now.setMinutes(59);
			now.setSeconds(59);
			deadTime = now.getTime();

			if (now.getDay() == 4) {
				deadTime += MmochatConstant.MS_PER_DAY;
			}
		} else {
			return;
		}

		MmochatWuDaoWinWords words = new MmochatWuDaoWinWords();
		words.setRoleId(req.getRoleId());
		words.setName(req.getName());
		words.setServerName(req.getServerName());
		words.setWords(req.getWord());
		words.setLevelRange(req.getLevelRange());
		words.setDeadTime(deadTime);
		MmochatNpcService.mixWuDaoWinWords.put(req.getRoleId(), words);
	}
}
